# -*- coding: utf-8 -*-
"""
This file is part of tptsig
Copyright © 2011 Telecom ParisTech, TSI
Auteur(s) : Liutkus Antoine
tptsig is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU LesserGeneral Public License
along with this program. If not, see <http://www.gnu.org/licenses/>."""

from scipy import *
from numpy import fft

import random

def nnrandn(shape,dtype="float64"):
	 return reshape([abs(random.gauss(0,1)) for x in range(0,prod(shape))],shape).astype(dtype);
		

def randnc(shape,dtype="float64"):
	 return reshape([random.gauss(0,1)+sqrt(-1)*random.gauss(0,1) for x in range(0,prod(shape))],shape).astype(dtype);
		

def randn(shape,dtype="float64"):
	 return reshape([random.gauss(0,1) for x in range(0,prod(shape))],shape).astype(dtype);
		
def normalizeMat(M):
	return array([x/sum(x) for x in M.T]).T;

def dynamicMat(M):
	Mtemp = array([x-min(x) for x in M.T]).T; 
	Mtemp = array([x/max(x) for x in Mtemp.T]).T; 
	return Mtemp;

def normalize(V):
	return V/sum(V);

def squeeze(a):
	b = asarray(a.shape)
	b = compress(not_equal(b,1),b)
	a.shape = tuple(b)
	return a

def gaussian(x,mu=0,sigma=1):
	return exp(-(x-mu)**2 / sigma**2);

def smooth(M, filterWidth):
	L = M.shape[0];
	smoothFilter = gaussian(linspace(-round(filterWidth)*2,round(filterWidth)*2,2*round(filterWidth)+1),0,filterWidth);
	smoothFilter/=sum(smoothFilter);
	if len(M.shape) > 1 :
		filteredM = array([convolve(x,smoothFilter) for x in M.T]).T;
		filteredM = filteredM[filterWidth:filterWidth+L,:];
	else :
		filteredM = convolve(M,smoothFilter);
		filteredM = filteredM[filterWidth:filterWidth+L];
		
	
	return filteredM;
	
